package com.education.welco.controller.teacherapp;

import com.education.common.utils.ApiReturnObject;
import com.education.welco.pojo.SCatalogMenu;
import com.education.welco.pojo.vo.SCatalogMenuVo;
import com.education.welco.service.ISCatalogMenuService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @Author: liftsail
 * @Date: 2022/11/30 17:19
 * @Description: 不积跬步无以至千里
 */
@RestController
@RequestMapping("/apptea/Catalog")
public class AppTeaCatalogController {

    @Autowired
    private ISCatalogMenuService catalogMenuService;

    //apptea端的课程目录结构(一二级)-lessonId-备课资源课程ID
    ///api/welco/sCatalogMenu/listOthers?catalogId=681    查询目录级别
    @GetMapping("/lessonCatalog")
    @ApiOperation(value = "课程目录查询", httpMethod = "GET")
    public ApiReturnObject lessonCatalog(@RequestParam(value = "lessonId", required = true) Integer lessonId) {
        //根据课程id 查询出之下的目录
        if (lessonId != null) {
            SCatalogMenu sCatalogMenu = new SCatalogMenu();
            //查询出第1级列表
            sCatalogMenu.setParentId(0);
            sCatalogMenu.setId(lessonId);
            SCatalogMenuVo list = catalogMenuService.selectSCatalogMenuList22(sCatalogMenu);
            //2级 以及 下面子列表
            SCatalogMenuVo sCatalogMenus = listSon2(list);
            if (sCatalogMenus.getChildren() == null) {
                return new ApiReturnObject("202", "暂无教学大纲，请先编辑教学大纲", "");
            }
            return new ApiReturnObject("200", "查询成功", sCatalogMenus);
        } else {
            return new ApiReturnObject("201", "缺少相关参数", "");
        }
    }

    //查询目录列表
    public SCatalogMenuVo listSon2(SCatalogMenuVo cList) {
        //根据课程id查询出一级目录
        List<SCatalogMenuVo> jrCatalogs = catalogMenuService.selectSCatalogMenuByParentId1(cList.getId());
        if (jrCatalogs != null && jrCatalogs.size() > 0) {
            //设置子级集合
            cList.setChildren(listSon(jrCatalogs));
        }
        return cList;
    }

    //查询目录列表
    public List<SCatalogMenuVo> listSon(List<SCatalogMenuVo> cList) {
        for (SCatalogMenuVo catalogMenu : cList) {
            //根据第一级的id查询出二级列表
            List<SCatalogMenuVo> jrCatalogs = catalogMenuService.selectSCatalogMenuByParentId1(catalogMenu.getId());
            if (jrCatalogs != null && jrCatalogs.size() > 0) {
                //设置子级集合
                catalogMenu.setChildren(listSon(jrCatalogs));
            }
        }
        return cList;
    }
}
